var rowselectPickingTransferInput;
CurrentPage.page = datagrid_PickingTransferInput(CurrentPage.id);
mainPage.add(CurrentPage.page);
mainPage.setActiveTab(CurrentPage.id);


function datagrid_PickingTransferInput(mod_id) {
    var Field =
	[
		'TRVHSEQ', 'TRVHTRANSCOD', 'TRVHTYPE', 'TRVHSITEFROM', 'TRVHSITETO', 'TRVHDATE', 'TRVHFLAG', 'TRVHCREA'
	];

    dataSource_PickingTransferInput = new WebApp.DataStore
	({
	    fields: Field
	});

    BindingData_PickingTransferInput('');

    var Grid_PickingTransferInput = new Ext.grid.EditorGridPanel
    ({
        //xtype: 'editorgrid',
        title: '',
        store: dataSource_PickingTransferInput,
        autoScroll: true,
        border: false,
        stripeRows: true,
        anchor: '100% 100%',
        trackMouseOver: false,
        loadMask: true,
        selModel: new Ext.grid.RowSelectionModel
        ({
            singleSelect: true,
            listeners:
            {
                rowselect: function (sm, row, rec) {
                    rowSelected_PickingTransferInput = dataSource_PickingTransferInput.getAt(row);
                    currentRowSelected_PickingTransferInput.row = row;
                    currentRowSelected_PickingTransferInput.data = rowSelected_PickingTransferInput;
                }
            }
        }),
        colModel: new Ext.grid.ColumnModel
        (
            [
				new Ext.grid.RowNumberer(),
				{
				    id: 'ColBarcode_PickingTransferInput',
				    header: 'No Transfer',
				    dataIndex: 'TRVHSEQ',
				    sortable: true,
				    width: 100,
				    editor: new Ext.form.TextField
					(
						{
						    id: 'fieldcolKodeMUMappMataUjuJurusan',
						    allowBlank: false,
						    enableKeyEvents: true,
						    listeners:
							{
							    // 'specialkey': function (field, e) {
							        // if (e.getKey() == e.ENTER) {
							            // var flag = false;
							            // if (this.getValue().length < 14) {
							                // flag = true;
							                // alert('kurang 14 buka windows form');
							            // }
							            // if (this.getValue().length == 14) {
							                // alert('masukan data barang ke grid');
							                // var query = "WHERE BARCODE = '" & this.getValue() & "'";
							                // BindingDataBarang_PickingTransferInput(query, rowSelected_PickingTransferInput, flag);
							            // }
							        // }
							    // }
							}
						}
					)
				},
				{
				    id: 'colItemID_PickingTransferInput',
				    header: 'From Store',
				    dataIndex: 'TRVHSITEFROM',
				    width: 220,
				    sortable: true
				},
				{
				    id: 'colSize_PickingTransferInput',
				    header: 'To Store',
				    dataIndex: 'TRVHSITETO',
				    width: 100,
				    sortable: true
				},
				{
				    id: 'coldate_PickingTransferInput',
				    header: 'Date',
				    dataIndex: 'TRVHDATE',
				    width: 100,
				    sortable: true
				},
				
				{
				    id: 'colstat_PickingTransferInput',
				    header: 'Status',
				    dataIndex: 'TRVHFLAG',
				    width: 100,
				    sortable: true
				}
            ]
        ),
        tbar: {
            xtype: 'toolbar',
            id: 'toolbar_PickingTransfer',
            items: [
            {
                xtype: 'button',
                text: 'Add Transaction',
                iconCls: '',
                tooltip: 'Add Transaction',
                id: 'btnAdd_PickingTransferInput',
                handler: function () {
                    if(rowselectPickingTransferInput !== undefined)
						{
							EntryPickingTransferInput(rowselectPickingTransferInput.data);
						}
						else
						{
							EntryPickingTransferInput();
						}
                }
            }
			/* ,
            {
                xtype: 'button',
                text: 'Confirm',
                iconCls: '',
                tooltip: 'Confirm Data',
                id: 'btnConfirm_PickingTransferInput',
                handler: function () {
                    pnlFormDataBasic_viSetAngota.getForm().submit
						({
						    //url: DataSave_viSetAngota(false),
						    waitMsg: 'Saving Data ... '
						});
                }
            } */
            ]
        },
        viewConfig: {
            forceFit: false
        }
    }
    );

    var mRecordSalesAddRow = Ext.data.Record.create
	(
		[
			'BARCODE'
		]
	);

    var FrmTabs_PickingTransferInput = new Ext.Panel
    (
		{
		    xtype: 'panel',
		    title: 'Picking Transfer',
		    layout: 'form',
		    closable: true,
		    iconCls: '',
		    id: mod_id,
		    shadhow: true,
		    margins: '0 5 5 0',
		    anchor: '100%',
		    items: [Grid_PickingTransferInput],
		    tbar:
			[
				
				'No Transfer' + ' : ', ' ',
				{
				    xtype: 'textfield',
				    fieldLabel: 'Nota no : ',
				    id: 'txtNoNota_PickingTransferInput',
				    anchor: '95%',
				    listeners:
					{
					    'specialkey': function (field, e) {
					        if (e.getKey() == e.ENTER) {

					        }
					    }
					}
				}, ' ', '-',
				'From Store' + ' : ', ' ',
				{
				    xtype: 'textfield',
				    fieldLabel: 'From Store : ',
				    id: 'txtfromstore_PickingTransferInput',
				    anchor: '95%',
				    listeners:
					{
					    'specialkey': function (field, e) {
					        if (e.getKey() == e.ENTER) {
					            alert('from store');
					        }
					    }
					}
				}, { xtype: 'tbfill' }, mLabelCount('lblKeteranganToko')
			],
		    listeners:
			{ 'afterrender': function () {
			    //alert('untuk mengambil data toko');
			}
			}
		}
    )

    return FrmTabs_PickingTransferInput;
}

function mLabelCount(nmLbl) {
    var lblCount = new Ext.form.Label
	(
		{
		    id: nmLbl,
		    text: 'Nama Toko'
		}
	)
    return lblCount;
}

function validationPickingTransferInput() {
    var x = 1;

    if (dataSource_PickingTransferInput.getCount == 0) {
        msgWarn_PickingTransferInput('Tidak ada data sales yang diinput', 'Sales Input');
        x = 0;
        alert('Validation Column Yang belum diisi');
    }

    return x;
}

function msgWarn_PickingTransferInput(msg, title) {
    Ext.MessageBox.show
    (
		{
		    title: title,
		    msg: msg,
		    buttons: Ext.MessageBox.OK,
		    icon: Ext.MessageBox.WARNING,
		    width: 250
		}
    )
}


function EntryPickingTransferInput(rowdata)
{
	var lebar=735;
	TRPickingTransfer = new Ext.Window
	(
		{
			id: 'PickingTransfer',
			title: " Create Transfer",
			closeAction: 'destroy',
			width: lebar,
			height: 400, 
			border: false,
			resizable:false,
			plain: true,
			layout: 'fit',
			iconCls: 'Penerimaan',
			modal: true,
			items: getFormPickingTransfer(lebar),
			listeners:
			{
				activate: function() 
				{					
				},
				afterShow: function() 
				{ 
					this.activate(); 
				},
				deactivate: function()
				{
					// rowSelected_PickingTransfer=undefined;
					 RefreshDataFilter_PickingTransfer(true);
				}
			}
		}
	);
	
	TRPickingTransfer.show();
	if (rowdata == undefined)
	{
		AddNew_PickingTransfer();
	}
	else
	{
		DataInit_PickingTransfer(rowdata)
	}	
	
};
 
 
 function AddNew_PickingTransfer()
 {
 
 }
 function getFormPickingTransfer(lebar) 
{
	var pnlTR_PickingTransfer = new Ext.FormPanel
	(
		{
			id: 'pnlTR_PickingTransfer',
			fileUpload: true,
			region: 'north',
			layout: 'fit',
			bodyStyle: 'padding:10px 10px 10px 10px',			
			anchor: '100%', 
			width:lebar,			
			border: false,			
			items: [getItemPickingTransfer(lebar)],
			tbar: 
			[
				{
					text: 'Add',
					id:'btnTambah',
					tooltip: 'Add New Record',
					iconCls: 'add',
					//handler: function() { TRDataAddNew_PickingTransfer(pnlTR_PickingTransfer) }
					handler: function() 
					{ 
						AddNew_PickingTransfer();
						ButtonDisabled_PickingTransfer(false);
					}
				}, '-', 
				{
					text: 'Save',
					id:'btnSimpan',
					tooltip: 'Save Data ',
					iconCls: 'save',
					//handler: function() { TRDatasave_PickingTransfer(pnlTR_PickingTransfer) }
					handler: function() 
					{ 
						Datasave_PickingTransfer(false);
						RefreshDataFilter_PickingTransfer(false);
					}
				}, '-', 
				{
					text: 'Hapus',
					id:'btnHapus',
					tooltip: 'Remove the selected item',
					iconCls: 'remove',
					handler: function() 
					{ 
						Ext.Msg.show
						(
							{
							   title:'Hapus',
							   msg: 'Apakah transaksi ini akan dihapus ?', 
							   buttons: Ext.MessageBox.YESNO,
							   fn: function (btn) 
							   {			
								   if (btn =='yes') 
									{
										DataDelete_PickingTransfer();
										RefreshDataFilter_PickingTransfer(false);									
									} 
							   },
							   icon: Ext.MessageBox.QUESTION
							}
						);
					}
				}
				
			]
		}
	);
		
	var GDtabDetail_PickingTransfer = new Ext.TabPanel
	(
		{
			region: 'center',
			id: 'GDtabDetail_PickingTransfer',
			activeTab: 0,
			//anchor: '100% 46%',
			anchor: '100% 48%',
			border: false,
			plain: true,
			defaults: 
			{
				autoScroll: false
			},
			items: GetDTLTRGrid_PickingTransfer(),
			tbar: 
			[
			{
				text: 'Tambah Baris',
				id:'btnTambahBaris',
				tooltip: 'Tambah Record Baru ',
				iconCls: 'add',				
				handler: function() 
				{ 
					TambahBaris_PickingTransfer();
				}
			}, '-', 
			{
				text: 'Hapus Baris',
				id:'btnHapusBaris',
				tooltip: 'Remove the selected item',
				iconCls: 'remove',
				handler: function()
					{
						if (dsDTLTRList_PickingTransfer.getCount() > 0 )
						{						
							if (cellSelectedDet_PickingTransfer != undefined)
							{
								if(CurrentTR_PickingTransfer != undefined)
								{
									HapusBaris_PickingTransfer();
								}
							}
							else
							{
								ShowPesanWarning_PickingTransfer('Silahkan pilih dahulu baris yang akan dihapus','Hapus baris');
							}
						}
					}
			}
		] 
	}
);
			
	var Formload_PickingTransfer = new Ext.Panel
	(
		{
			id: 'Formload_PickingTransfer',
			region: 'center',
			width:'100%',
			anchor:'100%',
			layout: 'form',
			title: '',
			bodyStyle: 'padding:15px',
			border: true,
			bodyStyle: 'background:#FFFFFF;',
			shadhow: true,
			iconCls: 'GL',
			items: [pnlTR_PickingTransfer, GDtabDetail_PickingTransfer]							  

		}
	);
	return Formload_PickingTransfer						
};
function getGridDetailPickingTransferInput()
{
	
	var Fields = [
		'TAHUN_ANGGARAN_TA',
		'KD_UNIT_KERJA',
		'KD_JNS_RKAT_JRKA',
		'NO_PROGRAM_PROG',
		'PRIORITAS',
		'NO_SP3D',
		'TGL_SP3D',
		'LINE',
		'ACCOUNT',
		'NAME',
		'DESKRIPSI',
		'VALUE',
		'POSTED',
		'DESKRIPSI'
	];
	dsGridDetailEntrySP3DForm = new WebApp.DataStore({ fields: Fields });	
	
	var grdDetailPickingTransferInput = new Ext.grid.EditorGridPanel(
		{
			id: 'grdDetailPickingTransferInput',
			border: false,
			region: 'center',
			anchor: '100% 60%',
			store: dsGridDetailEntrySP3DForm,
			autoScroll: true,
			viewConfig: { forceFit: true },
			sm: new Ext.grid.RowSelectionModel(
				{
					singleSelect: true,
					listeners:
					{
						'rowselect': function(sm, row, rec)
						{
							rowselectGridDetailEntrySP3DForm = dsGridDetailEntrySP3DForm.getAt(row);
							CurrentRowGridDetailEntrySP3DForm.row = row;
							CurrentRowGridDetailEntrySP3DForm.data = rowselectGridDetailEntrySP3DForm.data;
						}
					}
				}
			),
			cm: new Ext.grid.ColumnModel(
				[
					new Ext.grid.RowNumberer(),
					{
						xtype: 'gridcolumn',
						id: 'col_noakun_gldetail',
						dataIndex: 'ACCOUNT',
						header: 'No. Akun',
						width: 50
					},
					{
						xtype: 'gridcolumn',
						id: 'col_nmakun_gldetail',
						dataIndex: 'NAME',
						header: 'Nama Akun'/*,
						editor: new Ext.form.TextField(
							{
								id: 'fcol_nmakun_detsp3dform',
								enableKeyEvents: true,
								listeners:
								{
									'specialkey': function()
									{
										if(Ext.EventObject.getKey() === 13)
										{
											alert(this.getValue());
										}
									}
								}
							}
						)*/
					},
					{
						xtype: 'gridcolumn',
						id: 'col_des_gldetail',
						dataIndex: 'DESKRIPSI',
						header: 'Deskripsi',
						editor: new Ext.form.TextField(
							{
								id: 'fcol_des_detsp3dform'								
							}
						)
					},
					/* {
						xtype: 'gridcolumn',
						id: 'col_ket_gldetail',
						dataIndex: 'KETERANGAN',
						header: 'Keterangan',
						editor: new Ext.form.TextField(
							{
								id: 'fcol_ket_detsp3dform'								
							}
						)
					}, */
					{
						xtype: 'gridcolumn',
						id: 'col_jum_gldetail',
						dataIndex: 'VALUE',
						header: 'Jumlah',
						renderer: function(v, params, record)
						{
							var str = "<div style='white-space:normal; padding: 2px;text-align: right;'>" + formatCurrency(record.data.VALUE) + "</div>";
							return str;
						},
						editor: new Ext.form.TextField(
							{
								id: 'fcol_jum_detsp3dform',
								style: 'text-align: right;',
								enableKeyEvents: true,
								listeners:
								{
									'specialkey': function()
									{
										if(Ext.EventObject.getKey() === 13)
										{
											//CalcTotalDetailEntrySP3DForm();
										}
									},
									'blur' : function(){
										//CalcTotalDetailEntrySP3DForm();
									}
								}
							}
						)
					}
				]
			),
			tbar:
			[
				/* {
					xtype: 'button',
					id: 'btnAddRowDetailEntrySP3DForm',
					iconCls: 'add',
					text: 'Tambah Baris',
					handler: function(){ TambahBarisDetailEntrySP3DForm(); }
				}, */
				{
					xtype: 'button',
					id: 'btnDelRowDetailEntrySP3DForm',
					iconCls: 'remove',
					text: 'Hapus Baris',
					handler: function(){ HapusBarisDetailEntrySP3DForm(); }
				}
			],
			listeners:
			{
				'afterrender': function()
				{
					this.store.on("load",
						function()
						{
						//	CalcTotalDetailEntrySP3DForm()
						}
					);
					
					this.store.on("datachanged",
						function()
						{
							//CalcTotalDetailEntrySP3DForm()
						}
					);
				}
			}
		}
	);
	
	return grdDetailPickingTransferInput;
}

function getItemPickingTransfer(lebar)
{
	var items = 
	{
		layout:'fit',
		anchor:'100%',
		width: lebar - 36,
		height: 100,
		labelAlign: 'right',
		bodyStyle: 'padding:7px 7px 7px 7px',
		items:
		[
			{
				columnWidth:.9,
				width:lebar-36,
				layout: 'form',
				border:false,
				items: 
				[
					getItemPanelNo_PickingTransfer(lebar)//,
					
				]
			}
		]
	};
	return items;			
};

function getItemPanelNo_PickingTransfer(lebar)
{
	var items = 			
	{
	layout:'column',
	border:false,
	width:lebar,
	items:
		[
			{
				columnWidth:.8,
				layout: 'form',
				border:false,
				labelWidth:111,
				items: 
				[
				
				
					{
								xtype:'textfield',
								fieldLabel: 'No Transfer',
								name: 'txtnoPickingTransfer',
								id: 'txtnoPickingTransfer',
								readOnly:true,
								width:100,
								
							},
					{
						xtype: 'compositefield',
						fieldLabel: 'From Store',
						items: 
						[
							comboStorePickingTransfer(),
							{
								xtype:'displayfield',
								value:'To Store :'
							},
							comboStorePickingTransferTO()
						]
					}
				]
			}
		]
	}
	return items;	
};


function GetDTLTRGrid_PickingTransfer() 
{
	// grid untuk detail transaksi	
	var fldDetail = ['CSO_NUMBER', 'CSO_DATE', 'LINE', 'ACCOUNT', 'DESCRIPTION', 'VALUE', 'POSTED', 'NAMAACCOUNT']
	
	dsDTLTRList_PickingTransfer = new WebApp.DataStore({ fields: fldDetail })
	dsTmp_PickingTransfer= new WebApp.DataStore({ fields: fldDetail })
	
	var gridDTLTR_PickingTransfer = new Ext.grid.EditorGridPanel
	(
		{
			title: 'Detail ',
			stripeRows: true,
			store: dsDTLTRList_PickingTransfer,
			id:'Dttlgrid_PickingTransfer',
			border: false,
			columnLines:true,
			frame:true,
			anchor: '100% 100%',			
			sm: new Ext.grid.CellSelectionModel
			(
				{
					singleSelect: true,
					listeners: 
					{
						cellselect: function(sm, row, rec)
						{
							cellSelectedDet_PickingTransfer =dsDTLTRList_PickingTransfer.getAt(row);
							CurrentTR_PickingTransfer.row = row;
							//CurrentTRGL.data = cellSelectedGLDet;
						}
					}
				}
			),
			cm: TRDetailColumModel_PickingTransfer()
			, viewConfig: 
			{
				forceFit: true
			}
		}
	);
			
	return gridDTLTR_PickingTransfer;
};

			
function TRDetailColumModel_PickingTransfer() 
{
	return new Ext.grid.ColumnModel
	(
		[
			new Ext.grid.RowNumberer(), 
			{
				id: 'Account_PickingTransfer',
				name: 'Account_PickingTransfer',
				header: "No Barang",
				dataIndex: 'ACCOUNT',
				sortable: false,
				anchor: '10%',
				editor: new Ext.form.TextField
				(
					{
						id:'fieldAcc_PickingTransfer',
						allowBlank: false,
						enableKeyEvents : true,
						listeners: 
						{ 
							'specialkey' : function()
							{
								if (Ext.EventObject.getKey() === 13) 
								{
									var p = new mRecord_PickingTransfer
									(
										{
											ACCOUNT: '',
											NAMAACCOUNT: '',
											DESCRIPTION: Ext.get('txtCatatan_PickingTransfer').getValue(),
											VALUE: '',								
											LINE:''
										}
									);
									FormLookupAccount(" Where left(Account,1) in ('1','2','3','5')  and Account like '" 
										+ Ext.get('fieldAcc_PickingTransfer').dom.value + "%'  AND TYPE ='D'  ",dsDTLTRList_PickingTransfer,p,true,'',false);
								} 
							}
						}
					}
				),
				width: 70
			}, 
			{
				id: 'Name_PickingTransfer',
				name: 'Name_PickingTransfer',
				header: "Barang",
				dataIndex: 'NAMAACCOUNT',
				anchor: '30%',
				editor: new Ext.form.TextField
				(
					{
						id:'fieldAccName_PickingTransfer',
						allowBlank: false,
						enableKeyEvents : true,
						listeners: 
						{ 
							'specialkey' : function()
							{
								if (Ext.EventObject.getKey() === 13) 
								{
									var p = new mRecord_PickingTransfer
									(
										{
											ACCOUNT: '',
											NAMAACCOUNT: '',
											DESCRIPTION: Ext.get('txtCatatan_PickingTransfer').getValue(),		
											VALUE: '',								
											LINE:''
										}
									);
									FormLookupAccount(" Where left(Account,1) in ('1','2','3','5')  and Name like '%" 
										+ Ext.get('fieldAccName_PickingTransfer').dom.value + "%' AND TYPE ='D' ",dsDTLTRList_PickingTransfer,p,true,'',false);									
								} 
							}
						}
					}
				)
			}, 
			 
			{
				id: 'Value_PickingTransfer',				
				name: 'Value_PickingTransfer',
				header: "Jumlah",
				anchor: '15%',
				dataIndex: 'VALUE', 
				align:'right',
				
				listeners: 
				{ 
					
				}
			}
		]
	)
};


function comboStorePickingTransfer()
{
	var Field = ['STRCOD', 'STRDESC'];
	dsPickingTransfer = new WebApp.DataStore({ fields: Field });
	
  var comboPickingTransfer = new Ext.form.ComboBox
	(
		{
			id:'comboFromPickingTransfer',
			typeAhead: true,
			triggerAction: 'all',
			lazyRender:true,
			mode: 'local',
			//emptyText:'',
			fieldLabel: 'From Store ',			
			align:'Right',
			anchor:'96%',
			store: dsPickingTransfer,
			valueField: 'STRCOD',
			displayField: 'STRDESC',
			listeners:  
			{
				'select': function(a,b,c)
				{   
					
				} 
			}
		}
	);
	
	dsPickingTransfer.load
	(
		{
		    params:
			{
			    Skip: 0,
			    Take: 1000,
			    Sort: 'STRCOD',
			    Sortdir: 'ASC',
			    target: 'viCboUnitKerja',
			    param: ''
			}
		}
	);
	
	return comboPickingTransfer;
} ;



function comboStorePickingTransferTO()
{
	var Field = ['STRCOD', 'STRDESC'];
	dsPickingTransfer = new WebApp.DataStore({ fields: Field });
	
  var comboPickingTransfer = new Ext.form.ComboBox
	(
		{
			id:'comboTOPickingTransfer',
			typeAhead: true,
			triggerAction: 'all',
			lazyRender:true,
			mode: 'local',
			//emptyText:'',
			fieldLabel: 'From Store ',			
			align:'Right',
			anchor:'96%',
			store: dsPickingTransfer,
			valueField: 'STRCOD',
			displayField: 'STRDESC',
			listeners:  
			{
				'select': function(a,b,c)
				{   
					
				} 
			}
		}
	);
	
	dsPickingTransfer.load
	(
		{
		    params:
			{
			    Skip: 0,
			    Take: 1000,
			    Sort: 'STRCOD',
			    Sortdir: 'ASC',
			    target: 'viCboUnitKerja',
			    param: ''
			}
		}
	);
	
	return comboPickingTransfer;
} ;


